from tile import Tile
from tileManager import TileManager
from listener import Listener
import random

class Game:

    def __init__(self):
        self.state = 0
        self.currentPlayer = 0
        self.numberOfPlayers = 2
        self.currentStack = 0
        self.turnsUntilGameOver = -1
        self.realEstateMarket = 7
        self.numResidential = 0
        self.numGovernment = 0
        self.numCommercial = 0
        self.numIndustry = 0
        self.numAirport = 0
        self.numRestaurant = 0
        self.numOffice = 0
        self.numSchool = 0
        self.numSkyscraper = 0
        self.numDealership = 0
        self.hurdles = [15, 22, 29, 35, 41, 47, 53, 59, 64, 69, 74, 78, 82, 86, 89, 92, 95, 98, 101, 103, 105, 107, 109, 111, 113, 115, 117, 119, 121, 123, 125, 127, 129, 131, 133, 135, 137, 139, 141, 143, 145, 147, 149, 151, 153, 155, 157, 159, 161, 163, 165, 167, 169, 171, 173, 175, 177, 179]
        self.endLength = random.randint(12, 18)
        
        tm = TileManager()
        self.stack = tm.createTileLists(self.numberOfPlayers)

        self.communityPark = Tile(
            100, #id
            'Community Park', #name
            'None', #type
            'grey', #color
            -1, #stack
            4, #cost
            -1, #income
            0, #reputation
            0, #people
            0, #money
            3, #abilityID,
            'R', #abilityIRPM
            'IRC', #abilityModifier
            1, #abilityAffecters
            "communitypary.jpg"
        )      
        self.communityParkStack = [self.communityPark] * 4
        
        self.heavyFactory = Tile(
            101, #id
            'Heavy Factory', #name
            'None', #type
            'yellow', #color
            -1, #stack
            3, #cost
            1, #income
            0, #reputation
            0, #people
            0, #money
            3, #abilityID,
            'R', #abilityIRPM
            'GR', #abilityModifier
            -1, #abilityAffecters
            "heavyfactory.jpg"
        )
        self.heavyFactoryStack = [self.heavyFactory] * 4
        
        self.suburb = Tile(
            102, #id
            'Suburb', #name
            'None', #type
            'green', #color
            -1, #stack
            3, #cost
            0, #income
            0, #reputation
            2, #people
            0, #money
            0, #abilityID,
            0, #abilityIRPM
            0, #abilityModifier
            0, #abilityAffecters
            "suburbs.jpg"          
        )
        self.suburbStack = [self.suburb] * 4
        
        self.listeners = []
               
# states:
# 0 : initializing
# 1 : player 1 turn
# 2 : player 2 turn
# 3 : end state

    def update(self):
        if self.currentPlayer == 0:
            self.currentPlayer = 1
        else:
            self.currentPlayer = 0
            
        if len(self.stack) >= 62:
            self.currentStack = 0
        elif len(self.stack) >= 47:
            self.currentStack = 1
        else:
            self.currentStack = 2
                
    def is_game_over(self):
        return self.turnsUntilGameOver == 0
        
    def getCurrentPlayer(self):
        return self.currentPlayer
        
    def getStack(self):
        return self.stack
        
    def getCurrentStack(self):
        return self.currentStack
        
    def removeFromCurrentStack(self, index):
        stack = self.getStack()
        tile = stack[index]
        stack.remove(stack[index])
        return tile
        
    def getSuburbStack(self):
        return self.suburbStack
        
    def removeSuburb(self):
        self.suburbStack.pop()
        
    def getHeavyFactoryStack(self):
        return self.heavyFactoryStack

    def removeHeavyFactory(self):
        self.heavyFactoryStack.pop()
        
    def getCommunityParkStack(self):
        return self.communityParkStack
        
    def removeCommunityPark(self):
        self.communityParkStack.pop()
               
    def getNumberResidential(self):
        return self.numResidential
    
    def getNumberGovernment(self):
        return self.numGovernment
    
    def getNumberCommercial(self):
        return self.numCommercial
    
    def getNumberIndustry(self):
        return self.numIndustry
    
    def getNumberAirport(self):
        return self.numAirport
    
    def getNumberRestaurant(self):
        return self.numRestaurant
    
    def getNumberOffice(self):
        return self.numOffice
    
    def getNumberSchool(self):
        return self.numSchool
    
    def getNumberSkyscraper(self):
        return self.numSkyscraper
        
    def getSuburbTile(self):
        return self.suburb
        
    def getHeavyFactoryTile(self):
        return self.heavyFactory
        
    def getCommunityParkTile(self):
        return self.communityPark
        
    def getListeners(self):
        return self.listeners
        
    def addResidential(self):
        self.numResidential = self.numResidential + 1
        
    def addGovernment(self):
        self.numGovernment = self.numGovernment + 1
        
    def addCommercial(self):
        self.numCommercial = self.numCommercial + 1
        
    def addIndustry(self):
        self.numIndustry = self.numIndustry + 1
        
    def addAirport(self):
        self.numAirport = self.numAirport + 1
        
    def addRestaurant(self):
        self.numRestaurant = self.numRestaurant + 1
        
    def addOffice(self):
        self.numOffice = self.numOffice + 1
        
    def addSchool(self):
        self.numSchool = self.numSchool + 1
        
    def addSkyscraper(self):
        self.numSkyscraper = self.numSkyscraper + 1
        
    def addCarDealership(self):
        self.numDealership = self.numDealership + 1
        
    def addListener(self, listener):
        self.listeners.append(listener)

    def checkForHurdles(self, oldScore, newScore):
        iteration = newScore-oldScore
        i=1
        count = 0
        while i <= iteration:
            if oldScore +i in self.hurdles:
                count = count + 1
            i= i + 1
        return count

    def getTileAt(self, index):
        return self.stack[index]
    